home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / dino / dino_bot.1 / examples / complex / redblack.d < prev    next >
Encoding:
Text File  |  1991-04-15  |  3.0 KB  |  107 lines

  1. /* Copyright, 1990, Regents of the University of Colorado */
  2. /* This program uses a red-black algorithm to solve Poisson's equation,
  3.  * given rectangular boundary conditions.  It uses the example package
  4.  * of routines for dealing with uneven mappings in map.c. */
  5.  
  6. /* Defined size of the processor grid */
  7. #define P1 4
  8. #define P2 4
  9.  
  10. /* Defined size of the data grid */
  11. #define N1 8
  12. #define N2 8
  13.  
  14. /* Define the mapping for the data */
  15. map FivePoint = [block overlap 1,1][block overlap 1,1];
  16.  
  17. environment node[P1:id1][P2:id2] {
  18.  
  19. #include "../inc/map.c"
  20.  
  21.   composite iterate (a, in iter)
  22.  
  23.   double distributed a[N1][N2] map FivePoint;
  24.   int iter;
  25.  
  26.   {
  27.     map_var a1, a2;    /* Hold the looping information for this */
  28.     int q;              /* Looping variable */
  29.     int i, j;           /* Index variables */
  30.  
  31.     /* Set up the home variables for a */
  32.     set_map_var (N1, P1, id1, 1, 1, &a1);
  33.     set_map_var (N2, P2, id2, 1, 1, &a2);
  34.     limit_map_var (1, N1 - 2, &a1);
  35.     limit_map_var (1, N2 - 2, &a2);
  36.  
  37.     /* Go around iter times */
  38.     for (q = 0; q < iter; q++) {
  39.  
  40.       /* Update the information, if not the first iteration */
  41.       if (q > 0) {
  42.  
  43.         /* Send the home copy */
  44.         a[<a1.left,a1.right>][<a2.left,a2.right>]# =
  45.         a[<a1.left,a1.right>][<a2.left,a2.right>];
  46.  
  47.         /* Receive the data */
  48.         a[<a1.lover,a1.rover>][<a2.lover,a2.rover>] =
  49.         a[<a1.lover,a1.rover>][<a2.lover,a2.rover>]#;
  50.       }
  51.  
  52.       /* Now, let's recompute the even entries */
  53.       for (i = a1.left; i <= a1.right; i++)
  54.         for (j = ((((i + a2.left) % 2) == 1) ? 1 : 0) + a2.left;
  55.              j <= a2.right; j += 2)
  56.  
  57.           /* Do the recomputation */
  58.           a[i][j] = (a[i-1][j] + a[i+1][j] + a[i][j-1] + a[i][j+1]) / 4;
  59.  
  60.       /* Redistributed the information */
  61.       a[<a1.left,a1.right>][<a2.left,a2.right>]# =
  62.       a[<a1.left,a1.right>][<a2.left,a2.right>];
  63.  
  64.       a[<a1.lover,a1.rover>][<a2.lover,a2.rover>] =
  65.       a[<a1.lover,a1.rover>][<a2.lover,a2.rover>]#;
  66.  
  67.       /* Now, let's recompute the odd entries */
  68.       for (i = a1.left; i <= a1.right; i++)
  69.         for (j = ((((i + a2.left) % 2) == 0) ? 1 : 0) + a2.left;
  70.              j <= a2.right; j += 2)
  71.  
  72.           /* Do the recomputation */
  73.           a[i][j] = (a[i-1][j] + a[i+1][j] + a[i][j-1] + a[i][j+1]) / 4;
  74.     }
  75.   }
  76. }
  77. environment host {
  78.  
  79.   void main () {
  80.  
  81.     double m[N1][N2];           /* The array of data */
  82.     int i, j;                   /* Index variables */
  83.     int iter = 100;             /* Fake iteration variable */
  84.  
  85.     /* Set up the data in m */
  86.     for (i = 0; i < N1; i++)
  87.       for (j = 0; j < N2; j++)
  88.         m[i][j] = (i + 1) * (j + 1);
  89.     for (i = 1; i < N1 - 1; i++)
  90.       for (j = 1; j < N2 - 1; j++)
  91.         m[i][j] = 0;
  92.  
  93.  
  94.     /* Do the computation */
  95.     printf ("Running...\n");
  96.     iterate (m[][], iter)#;
  97.  
  98.     /* Print out the results */
  99.     for (i = 0; i < N1; i++) {
  100.       for (j = 0; j < N2; j++)
  101.         printf ("%6.2f ", m[i][j]);
  102.       printf ("\n");
  103.     }
  104.     printf ("\nThat's it!\n");
  105.   }
  106. }
  107.